package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.Brochure;
	import com.ettractions.kiosk.events.BrochureSelectEvent;
	import com.ettractions.kiosk.models.KioskModel;
	
	import flash.events.MouseEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Group;
	import spark.events.IndexChangeEvent;
	
	public class BrochureMediator extends Mediator
	{
		[Inject]
		public var model:KioskModel;
		
		[Inject]
		public var view:Brochure;
		
		private var _subviews:Vector.<Group>;
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(BrochureSelectEvent.BROCHURE_SELECTED, handleBrochureSelected);
			
			view.previousButton.addEventListener(MouseEvent.CLICK, handlePreviousNextButtonClick);	
			view.nextButton.addEventListener(MouseEvent.CLICK, handlePreviousNextButtonClick);			
			
			view.menu.selectedIndex = -1;
			view.menu.addEventListener(IndexChangeEvent.CHANGE, handleMenuIndexChange);
			
			_subviews = new <Group>[
				view.about,
				view.mapDirections,
				view.sendToMe,
				view.bookingInfo,
				view.specials
			];
			
			viewIndex = 0;
		}
		
		private function set viewIndex(index:int):void
		{
			view.menu.selectedIndex = index;
			
			for (var i:int=0; i<_subviews.length; i++)
				_subviews[i].visible = (i == index);
			
			view.media.visible = (index != 2);			
		}
		
		private function handleBrochureSelected(event:BrochureSelectEvent):void
		{
			if (event.brochure != null)
				viewIndex = 0;
		}
		
		private function handlePreviousNextButtonClick(event:MouseEvent):void
		{
			switch (event.target)
			{
				case view.previousButton:
					model.offsetBrochureIndex(-1);
					break;
				
				case view.nextButton:
					model.offsetBrochureIndex(1);					
					break;				
			}
		}
		
		private function handleMenuIndexChange(event:IndexChangeEvent):void
		{
			viewIndex = event.newIndex;
		}
	}
}